package palmtop.com.palmtop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.INaviInfoCallback;
import com.amap.api.navi.model.AMapNaviLocation;
import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.BootstrapButton;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;

import palmtop.com.palmtop.R;
import palmtop.com.palmtop.activity.DisleadertaskActivity;
import palmtop.com.palmtop.activity.ProcesstaskActivity;
import palmtop.com.palmtop.activity.ShowtaskgriddetailActivity;
import palmtop.com.palmtop.activity.TaskgridActivity;
import palmtop.com.palmtop.model.Processtask;
import palmtop.com.palmtop.model.Taskgrid;
import palmtop.com.palmtop.model.Unit;
import palmtop.com.palmtop.util.BadgeView;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

/**
 * Created by Administrator on 2017/9/1.
 */

public class HisTaskgridAdapter extends BaseAdapter {
    private ArrayList<Taskgrid> arrlist;
    private Context context;

    public HisTaskgridAdapter(ArrayList<Taskgrid> arrlist, Context context) {
        this.arrlist = arrlist;
        this.context = context;
    }

    @Override
    public int getCount() {
        return arrlist.size();
    }

    @Override
    public Object getItem(int i) {
        return arrlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int pos, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.item_histaskgrid,null);
        final AwesomeTextView taskname = view.findViewById(R.id.taskname);
        final AwesomeTextView taskname1 = view.findViewById(R.id.taskname1);
        AwesomeTextView taskaddress = view.findViewById(R.id.taskaddress);
        AwesomeTextView adddate = view.findViewById(R.id.adddate);



        final Taskgrid taskgrid = arrlist.get(pos);

        if(taskgrid.getTaskname().equals("")){
            taskname.setText(" ");
        }else{
            taskname.setText(taskgrid.getTaskname());
        }
//        taskname.setText(taskgrid.getTaskname());
        if(taskgrid.getTaskaddress().equals("")){
            taskaddress.setText(" ");
        }else{
            taskaddress.setText(taskgrid.getTaskaddress());
        }
//        taskaddress.setText(taskgrid.getTaskaddress());
        if(taskgrid.getAdddate().equals("")){
            adddate.setText(" ");
        }else{
            adddate.setText(taskgrid.getAdddate());
        }
//        adddate.setText(taskgrid.getAdddate());



       // BootstrapButton xiafa = view.findViewById(R.id.xiafa);
        Button daohang = view.findViewById(R.id.daohang);
        Button xiangqing = view.findViewById(R.id.xiangqing);
        /*if(taskgrid.getOrataskid()==0){
            xiafa.setEnabled(false);
        }else{
            //下发
            xiafa.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //社区民警下发任务
                    Intent i = new Intent();
                    i.putExtra("taskgrid",taskgrid);
                    i.setClass(context, DisleadertaskActivity.class);
                    context.startActivity(i);

                }
            });
        }*/


        //导航
        daohang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Poi end = new Poi("", new LatLng(Double.valueOf(taskgrid.getLatitude()), Double.valueOf(taskgrid.getLongitude())),"");
                AmapNaviParams mAmapNaviParams = new AmapNaviParams(null,null,end, AmapNaviType.DRIVER);
                mAmapNaviParams.setUseInnerVoice(true);
                AmapNaviPage.getInstance().showRouteActivity(context, mAmapNaviParams, new INaviInfoCallback() {
                    @Override
                    public void onInitNaviFailure() {

                    }

                    @Override
                    public void onGetNavigationText(String s) {

                    }

                    @Override
                    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

                    }

                    @Override
                    public void onArriveDestination(boolean b) {

                    }

                    @Override
                    public void onStartNavi(int i) {

                    }

                    @Override
                    public void onCalculateRouteSuccess(int[] ints) {

                    }

                    @Override
                    public void onCalculateRouteFailure(int i) {

                    }

                    @Override
                    public void onStopSpeaking() {

                    }

                    @Override
                    public void onReCalculateRoute(int i) {

                    }

                    @Override
                    public void onExitPage(int i) {

                    }

                    @Override
                    public void onStrategyChanged(int i) {

                    }

                    @Override
                    public View getCustomNaviBottomView() {
                        return null;
                    }

                    @Override
                    public View getCustomNaviView() {
                        return null;
                    }

                    @Override
                    public void onArrivedWayPoint(int i) {

                    }
                });

            }
        });
        //详情
        xiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*Intent i = new Intent();
                i.putExtra("taskgrid",taskgrid);
                i.setClass(context, ProcesstaskActivity.class);
                context.startActivity(i);*/

                Intent i = new Intent();
                i.putExtra("taskgrid",taskgrid);
                i.setClass(context, ShowtaskgriddetailActivity.class);
                context.startActivity(i);
            }
        });

        RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/getListByTaskidAndStatus");
        params.addBodyParameter("taskid", String.valueOf(taskgrid.getId()));
        params.addBodyParameter("status", "0");
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<Processtask>>() {
                }.getType();
                ArrayList<Processtask> list = gson.fromJson(result, listType);
                if (list!=null&&list.size() > 0) {
                    BadgeView badge = new BadgeView(context,taskname1);
                    badge.setText("待确认");
                    badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
                    badge.show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });


        return view;
    }
}
